package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.BaseAdapter;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.WorkAreaAdapter;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseSearchListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.LocaleWorkQueryCountQueryParams;
import com.isunland.managesystem.entity.Plotmappinginformation;
import com.isunland.managesystem.entity.PlotmappinginformationListOriginal;
import com.isunland.managesystem.entity.SimpleListParams;
import com.isunland.managesystem.entity.WorkAreaSearchParams;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/5 15:22
 */

public class WorkAreaSearchListFragment extends BaseSearchListFragment<Plotmappinginformation>
    implements WorkAreaAdapter.Callback {

  private static final int MENU_ITEM_SEARCH = 1;
  private static final int REQUEST_QUERY = 2;
  private WorkAreaSearchParams mParams;
  private LocaleWorkQueryCountQueryParams mParamsQuery;

  @Override protected BaseAdapter initAdapter(BaseVolleyActivity context, ArrayList list) {
    //noinspection unchecked
    return new WorkAreaAdapter(mActivity, mList, this, mParams.isShowSelect());
  }

  @Override protected SimpleListParams initParams() {
    mParams = mBaseParams instanceof WorkAreaSearchParams ? (WorkAreaSearchParams) mBaseParams
        : new WorkAreaSearchParams();
    mParamsQuery = new LocaleWorkQueryCountQueryParams();
    mParamsQuery.setOnlyShowOrg(true);
    //默认查询本会员下,可以查询所有.
    //如果是nodeClass或deptclass传manageOrgCode,否则传memberCode.
    mParamsQuery.setOrgCode(
        (MyStringUtil.equalsIgnoreCase(CurrentUser.NODE_CLASS, mCurrentUser.getRoleTypeFlag())
            || MyStringUtil.equalsIgnoreCase(CurrentUser.DEPT_CLASS,
            mCurrentUser.getRoleTypeFlag())) ? mCurrentUser.getManageOrgCode()
            : mCurrentUser.getMemberCode());
    mParamsQuery.setOrgnazition(mCurrentUser.getManageOrgName());
    SimpleListParams params = new SimpleListParams();

    params.setClassDetailActivity(PlotmappinginformationDetailActivity.class);
    params.setClassOriginal(PlotmappinginformationListOriginal.class);
    params.setTitle(MyStringUtil.connect(R.string.plot, R.string.lib, R.string.manage));
    params.setUrl(ApiConst.URL_QUERY_LIST_WORK_AREA);
    params.setDataStaus(DataStatus.NEW);
    return params;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("workId", mParams.getWorkId());
    params.put("worktype", mParams.getWorktype());
    //params.put("ifWork", MyStringUtil.valueOf(mParams.isIfWork()));
    params.put("longItude", mParams.getLongItude());
    params.put("latItude", mParams.getLatItude());
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("manageMemberCode", mCurrentUser.getManageOrgCode());
    params.put("selectMemberCode", mParamsQuery.getOrgCode());
    return params.getParams();
  }

  @Override protected String[] setQueryField() {
    //tplotName承包者,contactInformation联系方式,region区域,address地址
    return new String[] { "wplotDepict", "region", "tplotName", "contactInformation", "address" };
  }

  @Override public void click(View v, Plotmappinginformation item) {
    select(item);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.add(0, MENU_ITEM_SEARCH, 0, getString(R.string.query)).setIcon(R.drawable.search);
    menu.findItem(MENU_ITEM_SEARCH).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case MENU_ITEM_SEARCH:

        LocaleWorkQueryCountQueryActivity.newInstance(this, LocaleWorkQueryCountQueryActivity.class,
            mParamsQuery, REQUEST_QUERY);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_QUERY && data != null) {
      LocaleWorkQueryCountQueryParams params =
          (LocaleWorkQueryCountQueryParams) data.getSerializableExtra(BaseFragment.EXTRA_PARAMS);
      mParamsQuery = params == null ? new LocaleWorkQueryCountQueryParams() : params;
      volleyPost();
    }
  }
}
